package com.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.controller.vo.PersonVo;
import com.service.ILoginService;
import com.utils.Employee;
import com.utils.commons.LoginContextUtils;
import com.utils.commons.ResponseBean;
import com.utils.enums.ENUM_SUCESS;

@Controller
public class LoginController {

	@Autowired
	private ILoginService loginService;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	/**
	 * 验证帐号是否可用
	 * @author geliyang
	 * @param account
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/testAccount", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String testAccount(String account, String type) {
		if(loginService.hasAccount(account, type)) {
			return ResponseBean.of(null, ENUM_SUCESS.USED.value, "手机号已经注册过了");
		}
		return ResponseBean.of(null, ENUM_SUCESS.SUCESS.value);
	}
	
	/**
	 * 注册
	 * @param account
	 * @param password
	 * @param type
	 * @param valiCode
	 * @return
	 */
	@RequestMapping(value = "/register", produces = "text/html;charset=UTF-8")
	public String registerPerson(String account, String password, String type, String valiCode){
		String result;
		try {
			if(stringRedisTemplate.hasKey(account)) {
				ValueOperations<String, String> vop = stringRedisTemplate.opsForValue();
				if(!ObjectUtils.isEmpty(valiCode) && !valiCode.equals(vop.get(account))){
					return ResponseBean.of(null, ENUM_SUCESS.ERROR.value, "验证码错误");
				}
				stringRedisTemplate.delete(account);
			}else {
				return ResponseBean.of(null, ENUM_SUCESS.TIMEOUT.value, "验证码失效");
			}
			result = ResponseBean.of(loginService.registerPerson(account, password, null, type), ENUM_SUCESS.SUCESS.value, "注册成功");
		} catch (Exception e) {
			e.printStackTrace();
			result = ResponseBean.of(ENUM_SUCESS.FAILED.value);
		}
		return result;
	}
	
	@RequestMapping(value = "/login")
	public String loginPage(){
		return "indexpage/login";
	}
	
	@RequestMapping(value = "/loginout")
	public String loginout(HttpServletRequest request){
		LoginContextUtils.setLoginUser(null, request.getSession());
		return "indexpage/login";
	}
	
	@RequestMapping(value = "/api")
	public String apiPage(){
		return "apidoc/index";
	}
	
	/**
	 * 登录
	 * @param account
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/personLogin", produces = "text/html;charset=UTF-8")
	public String personLogin(String account, String password, ModelMap map, HttpServletRequest request){
		try {
			PersonVo ps = loginService.login(account, password);
			if(ps.getId() == null) {
				return "indexpage/login";
			}
			ps.setPassword(password);
			Employee emp = new Employee(ps);
			LoginContextUtils.setLoginUser(emp, request.getSession());
		} catch (Exception e) {
			return "indexpage/login";
		}
		return "redirect:/getApplyList";
	}
	
	@RequestMapping(value = "/forgotten", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String forgetPassword(String account, String password, String valiCode, String type){
		if(stringRedisTemplate.hasKey(account)) {
			ValueOperations<String, String> vop = stringRedisTemplate.opsForValue();
			if(!ObjectUtils.isEmpty(valiCode) && !valiCode.equals(vop.get(account))){
				return ResponseBean.of(null, ENUM_SUCESS.ERROR.value, "验证码错误");
			}
			stringRedisTemplate.delete(account);
		}else {
			return ResponseBean.of(null, ENUM_SUCESS.TIMEOUT.value, "验证码失效");
		}
		loginService.forgetPassword(account, password, type);
		return ResponseBean.of(null, ENUM_SUCESS.SUCESS.value, "修改密码成功");
	}
	
}
